package cn.iocoder.yudao.module.diy.service.diyroute;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.diy.utils.DiyRoutePageReqVO;
import cn.iocoder.yudao.module.diy.utils.DiyRouteSaveReqVO;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.other.DiyRouteListVo;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.other.DiyRouteSearchParam;
import cn.iocoder.yudao.module.diy.dal.diy.LinkEnum;
import cn.iocoder.yudao.module.diy.dal.diyroute.DiyRouteDO;
import cn.iocoder.yudao.module.diy.dal.mysql.diyroute.NDiyRouteMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.diy.enums.ErrorCodeConstants.DIY_ROUTE_NOT_EXISTS;

/**
 * 自定义路由 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DiyRouteServiceImpl implements DiyRouteService {

    @Resource
    private NDiyRouteMapper diyRouteMapper;

    @Override
    public Integer createDiyRoute(DiyRouteSaveReqVO createReqVO) {
        // 插入
        DiyRouteDO diyRoute = BeanUtils.toBean(createReqVO, DiyRouteDO.class);
        diyRouteMapper.insert(diyRoute);
        // 返回
        return diyRoute.getId();
    }

    @Override
    public void updateDiyRoute(DiyRouteSaveReqVO updateReqVO) {
        // 校验存在
        validateDiyRouteExists(updateReqVO.getId());
        // 更新
        DiyRouteDO updateObj = BeanUtils.toBean(updateReqVO, DiyRouteDO.class);
        diyRouteMapper.updateById(updateObj);
    }

    @Override
    public void deleteDiyRoute(Integer id) {
        // 校验存在
        validateDiyRouteExists(id);
        // 删除
        diyRouteMapper.deleteById(id);
    }

    private void validateDiyRouteExists(Integer id) {
        if (diyRouteMapper.selectById(id) == null) {
            throw exception(DIY_ROUTE_NOT_EXISTS);
        }
    }

    @Override
    public DiyRouteDO getDiyRoute(Integer id) {
        return diyRouteMapper.selectById(id);
    }

    @Override
    public PageResult<DiyRouteDO> getDiyRoutePage(DiyRoutePageReqVO pageReqVO) {
        return diyRouteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DiyRouteListVo> list(DiyRouteSearchParam searchParam) {
        JSONObject linkEnum = LinkEnum.getLink();

        List<DiyRouteListVo> routerList = new LinkedList<>();
        int sort = 0;
        for (String key : linkEnum.keySet()) {

            if (ObjectUtil.isNotEmpty(searchParam.getAddonName())) {
                String addonName = ObjectUtil.defaultIfNull(linkEnum.getByPath(key + ".addon_info.title", String.class), "");
                if (!addonName.equals(searchParam.getAddonName())) continue;
            }

            JSONArray linkArray = ObjectUtil.defaultIfNull(linkEnum.getByPath(key + ".child_list", JSONArray.class), new JSONArray());
            for (int i = 0; i < linkArray.size(); i++) {
                JSONObject link = linkArray.getJSONObject(i);

                if (ObjectUtil.isNotEmpty(searchParam.getUrl()) && !link.getStr("url").equals(searchParam.getUrl())) {
                    continue;
                }
                if (ObjectUtil.isNotEmpty(searchParam.getTitle()) && link.getStr("title").indexOf(searchParam.getTitle()) == -1) {
                    continue;
                }

                DiyRouteListVo vo = new DiyRouteListVo();
                vo.setName(link.getStr("name"));
                vo.setTitle(link.getStr("title"));
                vo.setAddonInfo(ObjectUtil.defaultIfNull(linkEnum.getByPath(key + ".addon_info", JSONObject.class), new JSONObject()));
                vo.setParent(key);
                vo.setPage(link.getStr("url"));
                vo.setIsShare(link.getInt("is_share"));
                vo.setAction(link.getStr("action"));
                vo.setSort(sort);
                routerList.add(vo);
                sort++;
            }
        }

        return routerList;
    }


}